﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.ServiceLocation;
using Subfaculty.Common.Interfaces.Services;
using Subfaculty.WebApplication.Translators;
using Telerik.Web.Mvc;
using Subfaculty.Common.Entities;
using Subfaculty.WebApplication.ViewModels;

namespace Subfaculty.WebApplication.Controllers
{
	public class DepartmentController : Controller
	{
		private IDepartmentRepository _departmentRepository;
		private DepartmentTranslator _departmentTranslator;

		public DepartmentController()
		{
			_departmentRepository = ServiceLocator.Current.GetInstance<IDepartmentRepository>();
			_departmentTranslator = new DepartmentTranslator();
		}
		//
		// GET: /Department/

		public ActionResult Index()
		{
			var departmentsPE = _departmentTranslator.Translate(_departmentRepository.GetAll());

			return View(departmentsPE);
		}

		[GridAction]
		public ActionResult _Index()
		{
			var departmentPEList = _departmentTranslator.Translate(_departmentRepository.GetAll());

			return View("Index",new GridModel(departmentPEList));
		}

		[GridAction]
		public ActionResult SelectAjaxEditing()
		{

			var departmentPe = _departmentTranslator.Translate(_departmentRepository.GetAll());

			return View("Index", new GridModel(departmentPe));
		}

		[AcceptVerbs(HttpVerbs.Post)]
		[GridAction]
		public ActionResult SaveAjaxEditing(int id)
		{
			Department department = _departmentRepository.Get(id);
			DepartmentViewModel departmentPE = _departmentTranslator.Translate(department);

			TryUpdateModel(departmentPE);

			_departmentRepository.Save(_departmentTranslator.TranslateBack(departmentPE, department));

			var departmentPeList = _departmentTranslator.Translate(_departmentRepository.GetAll());
			return View("Index", new GridModel(departmentPeList));
		}
		[AcceptVerbs(HttpVerbs.Post)]
		[GridAction]
		public ActionResult InsertAjaxEditing()
		{
			DepartmentViewModel pe = new DepartmentViewModel();

			//Perform model binding (fill the product properties and validate it).
			if (TryUpdateModel(pe))
			{
				Department department = new Department();
				//The model is valid - insert the product.
				_departmentRepository.Save(_departmentTranslator.TranslateBack(pe, department));
			}
			//Rebind the grid
			var disciplinesPeList = _departmentTranslator.Translate(_departmentRepository.GetAll());
			return View("Index", new GridModel(disciplinesPeList));
		}
		[AcceptVerbs(HttpVerbs.Post)]
		[GridAction]
		public ActionResult DeleteAjaxEditing(int id)
		{
			//Find a customer with ProductID equal to the id action parameter
			Department department = _departmentRepository.Get(id);
			if (department != null)
			{
				//Delete the record 
				_departmentRepository.Delete(department);
			}

			var departmentPeList = _departmentTranslator.Translate(_departmentRepository.GetAll());
			//Rebind the grid
			return View("Index", new GridModel(departmentPeList));
		}


	}
}
